package org.example.manager.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.example.manager.ShortLinkManager;
import org.example.mapper.ShortLinkMapper;
import org.example.model.ShortLinkDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description:
 * @author: 谢超
 * @date: 2023/8/17 11:49
 */
@Component
@Slf4j
public class ShortLinkManagerImpl implements ShortLinkManager {

    @Resource
    private ShortLinkMapper shortLinkMapper;

    @Override
    public int addShortLink(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.insert(shortLinkDO);
    }

    @Override
    public ShortLinkDO getShortLinkByCode(String shortLinkCode) {
        ShortLinkDO shortLinkDO = shortLinkMapper.selectOne(Wrappers.lambdaQuery(ShortLinkDO.class)
                .eq(ShortLinkDO::getCode, shortLinkCode)
                .eq(ShortLinkDO::getDel, 0));
        return shortLinkDO;
    }

    @Override
    public int deleteShortLink(ShortLinkDO shortLinkDO) {
        // 采用update的方式
        return shortLinkMapper.update(null, Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getAccountNo, shortLinkDO.getAccountNo())
                .eq(ShortLinkDO::getCode, shortLinkDO.getCode())
                .set(ShortLinkDO::getDel, 1)
        );
    }

    @Override
    public int update(ShortLinkDO shortLinkDO) {
        return shortLinkMapper.update(null, Wrappers.lambdaUpdate(ShortLinkDO.class)
                .eq(ShortLinkDO::getAccountNo, shortLinkDO.getAccountNo() )
                .eq(ShortLinkDO::getCode, shortLinkDO.getCode())
                .eq(ShortLinkDO::getDel, 0)
                .set(ShortLinkDO::getDomain, shortLinkDO.getDomain())
                .set(ShortLinkDO::getTitle, shortLinkDO.getTitle()));
    }
}
